
Security News
NVD Concedes Inability to Keep Pace with Surging CVE Disclosures in 2025
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
xml-formatter
Advanced tools
Converts a XML string into a human readable format (pretty print) while respecting the xml:space attribute
The xml-formatter npm package is a utility for formatting XML strings. It helps in making XML data more readable by properly indenting the XML elements. This can be particularly useful for debugging, logging, or displaying XML data in a user-friendly format.
Basic Formatting
This feature allows you to format a simple XML string, making it more readable by adding appropriate indentation.
const xmlFormatter = require('xml-formatter');
const xml = '<root><child>value</child></root>';
const formattedXml = xmlFormatter(xml);
console.log(formattedXml);
Custom Indentation
This feature allows you to specify custom indentation for the formatted XML string. In this example, two spaces are used for indentation.
const xmlFormatter = require('xml-formatter');
const xml = '<root><child>value</child></root>';
const formattedXml = xmlFormatter(xml, { indentation: ' ' });
console.log(formattedXml);
Removing XML Declarations
This feature allows you to remove XML declarations from the formatted XML string. This can be useful if you want a cleaner output without the XML declaration.
const xmlFormatter = require('xml-formatter');
const xml = '<?xml version="1.0"?><root><child>value</child></root>';
const formattedXml = xmlFormatter(xml, { stripComments: true });
console.log(formattedXml);
Handling Large XML Strings
This feature demonstrates the ability of xml-formatter to handle and format large XML strings efficiently.
const xmlFormatter = require('xml-formatter');
const largeXml = '<root>' + '<child>value</child>'.repeat(1000) + '</root>';
const formattedXml = xmlFormatter(largeXml);
console.log(formattedXml);
The pretty-data package provides similar functionality for formatting XML, JSON, and CSS data. It offers a broader range of data formatting options compared to xml-formatter, which is specialized for XML.
The xml2js package is primarily used for converting XML to JavaScript objects and vice versa. While it does offer some formatting capabilities, its main focus is on XML parsing and building, making it more versatile for data manipulation tasks.
The xmlbuilder package is designed for building XML documents using a simple and chainable API. It also includes formatting options, but its primary strength lies in creating XML documents programmatically.
Converts XML into a human readable format (pretty print) while respecting the xml:space attribute.
This module can also be used on the browser using the browserified version with a small footprint (8KB file size).
$ npm install xml-formatter
JavaScript:
var format = require('xml-formatter');
var xml = '<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>';
var formattedXml = format(xml);
console.log(formattedXml);
Output:
<root>
<content>
<p xml:space="preserve">This is <b>some</b> content.</p>
</content>
</root>
JavaScript:
var format = require('xml-formatter');
var xml = '<root><!-- content --><content><p>This is <b>some</b> content.</content></p>';
var formattedXml = format(xml, {
indentation: ' ',
filter: (node) => node.type !== 'Comment',
collapseContent: true,
lineSeparator: '\n'
});
console.log(formattedXml);
Output:
<root>
<content>
<p>This is <b>some</b> content.</p>
</content>
</root>
filter
(function(node)) Function to filter out unwanted nodes by returning false.indentation
(String, default=' '
) The value used for indentation.collapseContent
(Boolean, default=false
] True to keep content in the same line as the element. Only works if element contains at least one text nodelineSeparator
(String, default=\r\n
) Specify the line separator to usewhiteSpaceAtEndOfSelfclosingTag
(Boolean, default=false) to either end ad self closing tag with <tag/>
or <tag />
The code is transpiled using Babel with @babel/preset-env default values and bundled using browserify.
require('xml-formatter')
Page:
<script type="text/javascript" src="dist/browser/xml-formatter.js"></script>
Usage:
var xmlFormatter = require('xml-formatter');
var xml = '<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>';
var formattedXml = xmlFormatter(xml);
console.log(formattedXml);
xmlFormatter
<script type="text/javascript" src="dist/browser/xml-formatter-singleton.js"></script>
Usage:
var xml = '<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>';
var formattedXml = xmlFormatter(xml);
console.log(formattedXml);
<root>
<content>
<p xml:space="preserve">This is <b>some</b> content.</p>
</content>
</root>
MIT
FAQs
Converts a XML string into a human readable format (pretty print) while respecting the xml:space attribute
The npm package xml-formatter receives a total of 536,904 weekly downloads. As such, xml-formatter popularity was classified as popular.
We found that xml-formatter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
Security Fundamentals
Attackers use obfuscation to hide malware in open source packages. Learn how to spot these techniques across npm, PyPI, Maven, and more.
Security News
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.